using System.Runtime.CompilerServices;
using Pinwheel.Toolkits;

namespace Pinwheel.LSerialize
{
    public unsafe struct LStreamBufferSpan : IDisposable
    {
        LStreamBuffer* m_Buffer;
        public LStreamBuffer* Buffer => m_Buffer;

        public LStreamBufferSpan(LStreamBuffer* buffer)
        {
            m_Buffer = buffer;
        }
        
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static LStreamBufferSpan CreateSpan(byte* source, int size) => LStreamBuffer.CreateSpan(source, size);

        public void Dispose()
        {
            Native.Current.Free(m_Buffer);
        }
    }

    public unsafe partial struct LStreamBuffer
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static LStreamBufferSpan CreateSpan(byte* source, int size)
        {
            LStreamBuffer* buffer = Native.Current.AllocAndClear<LStreamBuffer>();
            buffer->head = source;
            buffer->length = size;
            buffer->index = 0;
            buffer->tail = size;
            return new LStreamBufferSpan(buffer);
        }

        public LStreamBufferSpan AsSpan(int size, int offset = 0)
        {
            LStreamBuffer* buffer = Native.Current.AllocAndClear<LStreamBuffer>();
            buffer->head = Current + offset;
            buffer->length = size;
            buffer->index = 0;
            buffer->tail = size;
            return new LStreamBufferSpan(buffer);
        }
        
        
    }
    
    
    
    

}

